package com.comdo.newup_loan.module.limit;

import com.comdo.newup_loan.http.RetrofitHelper;
import com.comdo.newup_loan.model.CarByCodeBean;
import com.comdo.newup_loan.model.CarCityItemBean;
import com.comdo.newup_loan.model.CarConfigsBean;
import com.comdo.newup_loan.model.Result;
import com.comdo.newup_loan.util.StringUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by liyuan on 2017/11/7.
 */

class CarLimitPersenter extends CarLimitContact.Presenter {

    @Override
    public void getConfigtList() {
        mView.showLoadingDialog("");
        mCompositeDisposable.add(RetrofitHelper.createMyApi().fillInValuation()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CarConfigsBean>>() {
                    @Override
                    public void accept(Result<CarConfigsBean> Result) throws Exception {
                        mView.dismissLoadingDialog();
                        if (Result.getCode() == 1) {
                            mView.getConfigtList(Result.getResult());
                        } else {
                            mView.getConfigFail(Result.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.dismissLoadingDialog();
                        mView.getConfigFail("请求失败");
                    }
                }));
    }

    @Override
    public void getCityList(int provinceId) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("provinceId", provinceId);
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        mView.showLoadingDialog("");
        mCompositeDisposable.add(RetrofitHelper.createMyApi().getCityList(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CarCityItemBean>>>() {
                    @Override
                    public void accept(Result<List<CarCityItemBean>> Result) throws Exception {
                        mView.dismissLoadingDialog();
                        if (Result.getCode() == 1) {
                            mView.getCityList(Result.getResult());
                        } else {
                            mView.getConfigFail(Result.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.getConfigFail(" 请求失败");
                        mView.dismissLoadingDialog();
                    }
                }));
    }

    @Override
    public void getByCarCode(String CarCode) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("carCode", CarCode);
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        mCompositeDisposable.add(RetrofitHelper.createMyApi().getByCarCode(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CarByCodeBean>>() {
                    @Override
                    public void accept(Result<CarByCodeBean> Result) throws Exception {
                        if (Result.getCode() == 1) {
                            mView.getByCarCode(Result.getResult());
                        } else {
                            mView.getConfigFail(Result.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.getConfigFail(" 请求失败");
                    }
                }));
    }

    @Override
    public void assess(int modelId, String regDate, Double mile, int cityId, String color) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("modelId", modelId);
        map.put("regDate", regDate);
        map.put("mile", mile);
        map.put("cityId", cityId);
        map.put("color", color);
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        mView.showLoadingDialog("");
        mCompositeDisposable.add(RetrofitHelper.createMyApi().assess(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<Double>>() {
                    @Override
                    public void accept(Result<Double> Result) throws Exception {
                        mView.dismissLoadingDialog();
                        if (Result.getCode() == 1) {
                            mView.assess(Result.getResult());
                        } else {
                            mView.getConfigFail(Result.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.getConfigFail(" 请求失败");
                        mView.dismissLoadingDialog();
                    }
                }));
    }
}
